<template>
  <div class="register-page">
    <div class="container">
      <h2>注册账号</h2>
      <el-form :model="registerForm" ref="registerForm" :rules="rules">
        <el-form-item prop="email">
          <el-input 
            v-model="registerForm.email" 
            placeholder="请输入邮箱"
            prefix-icon="Message"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="registerForm.password" 
            type="password" 
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input 
            v-model="registerForm.confirmPassword" 
            type="password" 
            placeholder="确认密码"
            prefix-icon="Lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="register-btn" @click="handleRegister" :loading="loading">
          注册
        </el-button>
        <div class="links">
          <el-button link @click="goToLogin">返回登录</el-button>
        </div>
      </el-form>

      <div class="registered-accounts" v-if="accounts.length">
        <h3>已注册账号</h3>
        <el-table :data="accounts" style="width: 100%">
          <el-table-column prop="email" label="邮箱"></el-table-column>
          <el-table-column prop="createTime" label="注册时间"></el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

const router = useRouter();
const loading = ref(false);
const accounts = ref([]);

const registerForm = reactive({
  email: '',
  password: '',
  confirmPassword: ''
});

const rules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      },
      trigger: 'blur'
    }
  ]
};

const handleRegister = async () => {
  loading.value = true;
  try {
    // 这里添加注册API调用
    ElMessage.success('注册成功');
    router.push('/login');
  } catch (error) {
    ElMessage.error('注册失败：' + error.message);
  } finally {
    loading.value = false;
  }
};

const goToLogin = () => {
  router.push('/login');
};
</script>

<style scoped>
.register-page {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}

.container {
  width: 400px;
  padding: 30px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

h2 {
  text-align: center;
  color: #333;
  margin-bottom: 30px;
}

.register-btn {
  width: 100%;
  margin-top: 20px;
}

.links {
  text-align: center;
  margin-top: 20px;
}

.registered-accounts {
  margin-top: 30px;
}

h3 {
  margin-bottom: 15px;
}
</style>